# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023-01-11
desc: 
sample: 
"""
import csv
import hashlib
import json
import os
import random
import time

import allure
import pytest
import requests

from util import get_project_path, getcsv_list, getcsv_listdict

"""
普通的GET类型接口	GET	/pinter/com/getSku	id=1
http://82.156.74.26:9088/pinter/doc
"""

host = 'http://82.156.74.26:9088'

#100组，1000组参数覆盖测试
#list
path =os.path.join(get_project_path(),"files","sku.csv")

def setup():
   #数据库清空
   print("setup----")

def teardown():
    #造数据
   print("teardown----")

#方法级别
def setup_function():
   #数据库清空
   print("setup---- setup_function")

def teardown_function():
    #造数据
   print("teardown---- teardown_function")

# data_1 = [x for x in range(10)]
# data_2 = [x for x in range(100)]
# data_3 = [x for x in range(100)]
# @pytest.mark.parametrize('a', data_1)
# @pytest.mark.parametrize('b', data_2)
# @pytest.mark.parametrize('c', data_3)
# def test_parametrize_1(a, b,c):
#  #sleep(0.01)
#  print(f'笛卡尔积 测试数据为 ： {a}，{b},{c}')

# @pytest.mark.flaky(reruns=3,reruns_delay=2)
# @pytest.mark.parametrize("id,message",getcsv_list(path))
# def test_get1(id,message):
#     # url = host + '/pinter/com/getSku?id=1'
#     url = host + '/pinter/com/getSku'
#     params = {
#         "id": id
#     }
#     response = requests.get(url=url, params=params)
#     print(f"是否成功{response.status_code}")
#     print(f"返回结果{response.text}")
#     assert response.status_code == 200
#     if (response.status_code == 200):
#         # response.text str 不是字典
#         res_dict = json.loads(response.text)  # json str-dic
#         # json字典
#         res_dict = response.json()
#         assert res_dict["code"] == '0'
#         assert res_dict["message"] == message
#
#
# @pytest.mark.flaky(reruns=3,reruns_delay=2)
# @pytest.mark.parametrize("param",getcsv_listdict(path))
# def test_get2(param):
#     # url = host + '/pinter/com/getSku?id=1'
#     url = host + '/pinter/com/getSku'
#     params = {
#         "id": param.get('id')
#     }
#     response = requests.get(url=url, params=params)
#     print(f"是否成功{response.status_code}")
#     print(f"返回结果{response.text}")
#     assert response.status_code == 200
#     if (response.status_code == 200):
#         # response.text str 不是字典
#         res_dict = json.loads(response.text)  # json str-dic
#         # json字典
#         res_dict = response.json()
#         assert res_dict["code"] == '0'
#         assert res_dict["message"] == param.get('message')

def test_post():
    """
    post 参数k-v
    """
    url = host + '/pinter/com/login'
    data = {
        "userName": 'admin',
        'password': '1234'
    }
    response = requests.post(url=url, data=data)
    print(f"是否成功{response.status_code}")
    print(f"返回结果{response.text}")



@allure.parent_suite('我是parent_suite')
@allure.suite('我是suite')
@allure.sub_suite('我是sub_suite')
def test_postjson():
    """
    参数为json的POST接口
    :return:
    """
    url = host + '/pinter/com/register'
    data = {"userName": "test", "password": "1234",
            "gender": 1, "phoneNum": "110", "email": "beihe@163.com", "address": "Beijing"}
    # headers = {
    #     'Content-Type': 'application/json'
    # }
    response = requests.post(url=url, json=data)
    print(f"是否成功{response.status_code}")
    print(f"返回结果{response.text}")


def test_postkjson():
    url = host + '/pinter/com/buy'
    # data = {'param': {"skuId": 123, "num": 10}}
    data = {"param": '{"skuId": 123, "num": 10}'}
    response = requests.post(url=url, data=data)
    print(f"是否成功{response.status_code}")
    print(f"返回结果{response.text}")


def test_file():
    url = host + '/pinter/file/api/upload2'  # 文件上传
    file = {'file': open(r'D:\token.har', 'rb')}
    response = requests.post(url=url, files=file)
    print(f"是否成功{response.status_code}")
    print(f"返回结果{response.text}")

@pytest.mark.run(order=1)
def test_md5():
    url = host + '/pinter/com/userInfo'  # 文件加密
    # 11位 前三位厂商
    phoneNumber = ["130", "131", "138", "145", "146", "147", "150", "151", "152", "172", "174", "175", "180", "181",
                   "182", "198"]
    phone = random.choice(phoneNumber) + str(random.randint(10000000, 99999999))
    print(phone)
    ts = time.time()
    signtime = round(ts * 1000)
    print(signtime)
    # md5 摘要 指纹 hash
    # phone+'加盐'+'time'
    toMd5 = f"{phone}testfan{signtime}"
    data = {"phoneNum": "123434", "optCode": "testfan", "timestamp": "12112121212", "sign": "your sign data"}
    data["phoneNum"] = phone
    data["timestamp"] = signtime
    data["sign"] = md5(toMd5)
    response = requests.post(url=url, json=data)
    print(f"是否成功{response.status_code}")
    print(f"返回结果{response.text}")


def md5(toMd5):
    return hashlib.md5(toMd5.encode("utf-8")).hexdigest()
    # md5 = hashlib.md5()
    # md5.update(toMd5.encode("utf-8"))
    # return md5.hexdigest()


if __name__ == '__main__':
    # test_get()
    # test_post()
    # test_postjson()
    # test_postkjson()
    # test_file()
    #test_md5()
    pass
